<?php if (empty($this->data)) : // ถ้าไม่มีข้อมูล ?>
    <?php echo $this->element('dataNotFound'); ?>
<? else : // ถ้ามีข้อมูล ?>
    <?php $paginator->options(array('update' => 'content', 'indicator' => 'indicator', 'url' => array('controller' => 'CustProductSites', 'action' => 'display'))); ?>
    <?php echo $this->element('dataCaptionLimit', array('controller' => 'cust_product_sites')); ?>
    <?php $cur = $paginator->counter(array('format' => '%start%')); ?>
<table class="data">
    <tr>
        <th class="no"><?php echo $paginator->sort('No', 'id'); ?></th>
        <th>Stock Code</th>
        <th>Vendor Part no.</th>
        <th>Description</th>
            <?php foreach ($sites as $site) : ?>
        <th><?php echo $site['Site']['name']; ?></th>
            <?php endforeach; ?>
        <th>Total</th>
    </tr>
        <?php
        $site_id = $session->read('Auth.User.site_id');
        foreach($this->data as $i => $data): ?>
    <tr class="<?php echo (($i + 1) % 2 == 0)? 'even-cell' : 'odd-cell'; ?>">
        <td class="center"><?php echo $i + 1; ?></td>
        <td><?php echo !empty($data[$site_id])? 
                    $ajax->link($data[$site_id]['CustProductSite']['stock_code'],
                    '/cust_product_sites/view/' . $data[$site_id]['CustProductSite']['id'],
                    array('indicator' => 'indicator', 'update' => 'content')): '-'; ?></td>
        <td>
                    <?php
                    // หาว่า Product นี้ Dispatch from มาจากไหน
                    $dispatch_from;
                    // Site แรกที่มี CustProductSite ใน Product นี้
                    $first_site_id;
                    $total_stock = 0;
                    for ($s = 0; $s < count($sites); ++$s) {
                        if (!empty($data[$sites[$s]['Site']['id']])) {
                            $dispatch_from = $data[$sites[$s]['Site']['id']]['CustProductSite']['dispatch_from'];
                            $first_site_id = $sites[$s]['Site']['id'];
                            break;
                        }
                    }
                    echo $dispatch_from == ITS_PREFIX? $data['Product']['part_no']: $data['Product']['supplier_part_no']; ?>
        </td>
        <td><?php echo !empty($data[$first_site_id]['CustProductSite']['text'])? $data[$first_site_id]['CustProductSite']['text']: $data['Product']['title']; ?></td>
                <?php foreach ($sites as $site) : ?>
        <td class="text-right">
                        <?php if (!empty($data[$site['Site']['id']])) {
                            echo $data[$site['Site']['id']]['CustProductSite']['current_stock'];
                            $total_stock += $data[$site['Site']['id']]['CustProductSite']['current_stock'];
                        } else {
                            echo '-';
                        } ?>
        </td>
                <?php endforeach; ?>
        <td class="text-right bold"><?php echo $total_stock; ?></td>
    </tr>
        <?php endforeach; ?>
</table>
    <?php echo $this->element('paging'); ?>
<? endif; ?>
<?php //debug($this->data); ?>
<?php //debug($sites); ?>